<template>
  <el-dialog :visible.sync="visible" :title="!dataForm.id ? $t('add') : $t('update')" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form v-loading="loading" :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmitHandle()" :label-width="$i18n.locale === 'en-US' ? '120px' : '80px'">
      <el-tabs v-model="activeName" tab-position="left">
        <el-tab-pane label="基本设置" name="first">
          <el-col :span="24">

            <el-col :span="24">
              <el-form-item :label="'官方模板'" prop="titleWx">
                <!--文本框-->
                <el-input v-model="dataForm.titleWx" :placeholder="请选择模板" @focus="wechatTemplateDialogShow">
                </el-input>
              </el-form-item>
            </el-col>
            <!-- <el-col :span="24">
              <el-form-item :label="$t('msgWechatTemplate.appId')" prop="appId">
                <el-select v-model="dataForm.appId" filterable :placeholder="$t('msgWechatTemplate.appId')"
                  style="width:100%" clearable>
                  <el-option v-for="app in appList" :key="app.id" :label="app.appName" :value="app.appId"></el-option>
                </el-select>
              </el-form-item>
            </el-col> -->
            <el-col :span="24">
              <el-form-item :label="$t('msgWechatTemplate.templateId')" prop="templateId">
                <!--文本框-->
                <el-input v-model="dataForm.templateId" :placeholder="$t('msgWechatTemplate.templateId')" clearable>
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item :label="$t('msgWechatTemplate.templateName')" prop="templateName">
                <!--文本框-->
                <el-input v-model="dataForm.templateName" :placeholder="$t('msgWechatTemplate.templateName')" clearable>
                </el-input>
              </el-form-item>
            </el-col>
            <!-- <el-divider></el-divider> -->
            <el-col :span="24">
              <el-form-item :label="$t('common.remark')" prop="remark">
                <!--文本框-->
                <el-input type="textarea" :rows="3" v-model="dataForm.remark" :placeholder="$t('common.remark')" clearable>
                </el-input>
              </el-form-item>
            </el-col>
          </el-col>
        </el-tab-pane>
        <el-tab-pane label="模板设置" name="second">
          <el-col :span="24">
            <el-form-item :label="item.label" v-for="(item,index) in propertys" :key="index">
              <el-row>
                <el-col :span="16">
                  <el-input v-model="item.value"></el-input>
                </el-col>
                <el-col :span="8">
                  <el-color-picker v-model="item.color"></el-color-picker>
                </el-col>
              </el-row>
            </el-form-item>
          </el-col>
        </el-tab-pane>
      </el-tabs>
    </el-form>

    <template slot="footer">
      <el-button @click="visible = false">{{ $t('cancel') }}</el-button>
      <el-button type="primary" :loading="loading" @click="dataFormSubmitHandle()">{{ $t('confirm') }}</el-button>
    </template>
    <wechatTemplateDialog v-if="wechatTemplateDialogVisible" ref="wechatTemplateDialog" @backFun="backFun">
    </wechatTemplateDialog>
  </el-dialog>
</template>

<script>
import debounce from 'lodash/debounce'
import wechatTemplateDialog from './wechatTemplate-dialog'
export default {
  components: { wechatTemplateDialog },
  data () {
    return {
      wechatTemplateDialogVisible: false,
      loading: false,
      visible: false,
      appList: [],
      propertys: [],
      activeName: 'first',
      dataForm: {
        id: '',
        appId: 'EDP',
        signatureCode: '',
        templateId: '',
        templateName: '',
        content: '',
        remark: '',
        creator: '',
        createDate: '',
        updater: '',
        updateDate: '',
        accountId: '',
        templateIdWx: '',
        titleWx: ''
      }
    }
  },
  computed: {
    dataRule () {
      return {
        appId: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        signatureCode: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        templateId: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        templateName: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ]
      }
    },
    exampleDiv () {
      // console.log(this.dataForm)
      return this.dataForm.example.replace(/\n/g, '<br/>')
    }
    // propertys () {
    //   console.log(this.dataForm.content)
    //   if (this.dataForm.content) {
    //     return JSON.parse(this.dataForm.content)
    //   }
    // }
  },
  methods: {
    backFun (row) {
      this.$http.get(`/msg/wechatTemplate/${row.id}`).then(({ data: res }) => {
        this.loading = false
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        row = res.data
        this.dataForm.accountId = row.accountId
        this.dataForm.templateIdWx = row.templateIdWx
        this.dataForm.titleWx = row.title
        this.propertys = row.changeDefaultRoles
        this.dataForm.templateName = row.title
        this.wechatTemplateDialogVisible = false
      }).catch(() => { })
    },
    wechatTemplateDialogShow () {
      this.wechatTemplateDialogVisible = true
      this.$nextTick(() => {
        this.$refs.wechatTemplateDialog.init()
      })
    },
    init () {
      this.visible = true
      // this.getAppList()
      this.propertys = []
      // this.dataForm = {
      //   id: '',
      //   appId: '',
      //   signatureCode: '',
      //   templateId: '',
      //   templateName: '',
      //   content: '',
      //   remark: '',
      //   creator: '',
      //   createDate: '',
      //   updater: '',
      //   updateDate: '',
      //   accountId: '',
      //   templateIdWx: '',
      //   titleWx: ''
      // }
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.loading = true
          this.getInfo()
        }
      })
    },
    // 获取APP
    // getAppList () {
    //   return this.$http.get('/sys/sysApp/list').then(({ data: res }) => {
    //     if (res.code !== 0) {
    //       return this.$message.error(res.msg)
    //     }
    //     this.appList = res.data
    //   }).catch(() => { })
    // },
    // 获取信息
    getInfo () {
      this.loading = true
      this.$http.get(`/msg/msgWechatTemplate/${this.dataForm.id}`).then(({ data: res }) => {
        this.loading = false
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.dataForm = {
          ...this.dataForm,
          ...res.data
        }
        this.propertys = JSON.parse(this.dataForm.content)
      }).catch(() => { })
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        this.dataForm.content = JSON.stringify(this.propertys)
        this.loading = true
        this.$http[!this.dataForm.id ? 'post' : 'put']('/msg/msgWechatTemplate/', this.dataForm).then(({ data: res }) => {
          this.loading = false
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.visible = false
              this.$emit('refreshDataList')
            }
          })
        }).catch(() => { })
      })
    }, 1000, { 'leading': true, 'trailing': false })
  }
}
</script>
<style scoped>
.tmplmsg__detail-showcase {
  float: left;
  margin-right: 70px;
  line-height: 1.6;
  font-family: -apple-system-font, BlinkMacSystemFont, 'Helvetica Neue', 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei UI', 'Microsoft YaHei', Arial, sans-serif;
  color: #353535;
  font-size: 14px;
  -webkit-font-smoothing: antialiased;
}

.tmplmsg__showcase {
  width: 320px;
  border: 1px solid #e4e8eb;
}

.tmplmsg__showcase-wrap {
  padding: 18px 20px;
}

.tmplmsg__showcase-link {
  padding: 0 20px;
  border-top: 1px solid #e4e8eb;
  line-height: 44px;
  position: relative;
  color: #9a9a9a;
}

.tmplmsg__showcase-notice {
  text-align: center;
  color: #9a9a9a;
  padding-top: 30px;
}
</style>
